package com.liyuncong.algorithm.algorithm_similarity.impl;

import com.liyuncong.algorithm.algorithm_similarity.entity.Vector;
import com.liyuncong.algorithm.algorithm_similarity.similarity.ComputeSimilarity;
import com.liyuncong.algorithm.algorithm_similarity.util.VectorUtil;
/**
 * 通过欧式距离得到两向量之间的相似度
 * @author yuncong
 *
 */
public class EuclideanDistance implements ComputeSimilarity {

	public float compute(Vector vector1, Vector vector2) {
		VectorUtil.validateVectorLength(vector1, vector2);
		int vectorLength = vector1.getLength();
		// 向量对应元素差的平方和
		float quadraticSum = 0;
		for(int i = 0; i < vectorLength; i++) {
			float minus = vector1.valueOf(i) - vector2.valueOf(i);
			float minusSquare = (float) Math.pow(minus, 2);
			quadraticSum += minusSquare;
		}
		return (float) Math.sqrt(quadraticSum);
	}
	
	public static void main(String[] args) {
	}
}
